﻿using Admin2024.Domain.Entity.System;
using Admin2024.Domain.IDomainService.System.IUserService;
using Admin2024.Domain.Interface;
using Admin2024.Domain.ObjValue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Domain.DomainService.System.UserService
{
    public class UserStateManager : IUserStateManager
    {

        private readonly IRepository<AppUser> UserRep;
        private readonly IBaseDomainService<AppUser> baseDomainService;

        public UserStateManager(IBaseDomainService<AppUser> baseDomainService, IRepository<AppUser> UserRep)
        {
            this.baseDomainService = baseDomainService;
            this.UserRep = UserRep;
        }
        //批量删除
        public async Task<DomainResult<IEnumerable<string>>> DeleteRangeAsync(IEnumerable<Guid> ids)
        {
            if (ids == null || !ids.Any())
            {
                return DomainResult<IEnumerable<string>>.Error("没有提供要删除的用户ID");
            }

            try
            {
                // 使用通用存储接口的批量删除方法
                await UserRep.DeleteRangeAsync(ids);

                // 返回成功结果及删除的用户ID列表
                return DomainResult<IEnumerable<string>>.Success(ids.Select(id => id.ToString()));
            }
            catch (Exception ex)
            {
                // 处理异常并返回失败结果
                return DomainResult<IEnumerable<string>>.Error(ex.Message);
            }
        }



        //删除
        public async Task<DomainResult<AppUser>> DeleteUserAsync(Guid id)
        {
            var user = await baseDomainService.DeleteAsync(id);
            if (!user.IsSuccess) {
                return DomainResult<AppUser>.Error(user.Message);
            }
            return DomainResult<AppUser>.Success(user.Data);
        }
        //软删除
        public async Task<DomainResult<AppUser>> DisableUserAsync(Guid id)
        {
            var user = await baseDomainService.DisableAsync(id);
            if (!user.IsSuccess)
            {
                return DomainResult<AppUser>.Error(user.Message);
            }
            return DomainResult<AppUser>.Success(user.Data);
        }
        //启用 / 禁用
        public async Task<DomainResult<AppUser>> EnableUserAsync(Guid id)
        {

            var user = await baseDomainService.EnableAsync(id);
            if (!user.IsSuccess)
            {
                return DomainResult<AppUser>.Error(user.Message);
            }
            return DomainResult<AppUser>.Success(user.Data);
        }
    }
}
